<template>
  <!-- 修改为使用 :model-value 和 @update:model-value 实现双向绑定 -->
  <el-dialog :title="'报价信息'" :model-value="visible" :close-on-click-modal="false" @update:model-value="visible = $event"
    width="1200px" append-to-body @open="open">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :isRefresh="false" :search="false" :columns="columns"></right-toolbar>
    </el-row>
    <!-- 表格数据 -->
    <el-table ref="tableRef" v-loading="loading" size="large" max-height="600px" :indent="50" :data="dataList" row-key="autoBillID"
      :tree-props="{ children: 'childMaterial' }">
      <el-table-column type="index" label="序号" width="60" align="center">
        <template #default="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" v-if="columns[0].visible" prop="billName" align="left" min-width="300" />
      <el-table-column label="物料号" v-if="columns[1].visible" prop="billMaterialNO" align="center" width="150" />
      <el-table-column label="规格" v-if="columns[2].visible" prop="billSpecifications" align="center" width="200" />
      <el-table-column label="型号" v-if="columns[3].visible" prop="billTypes" align="center" width="200" />
      <el-table-column label="数量" v-if="columns[4].visible" prop="billRemaining" align="center" width="80" />
      <el-table-column label="单位" v-if="columns[5].visible" prop="billUnit" align="center" width="80" />
      <el-table-column label="采购备注" v-if="columns[6].visible" prop="billManufacturer" align="center" width="150" />
      <el-table-column label="成本(元)" v-if="columns[7].visible" prop="unitCost" align="center" width="80">
        <template #default="scope">
          <span class="red">{{ getTotalCost(scope.row,'unitCost') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总成本(元)" v-if="columns[8].visible" prop="totalCost" align="center" width="100">
        <template #default="scope">
          <span :class="[scope.row.status === 1 ? 'red' : '']">
            {{ getTotalCost(scope.row,'totalCost') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="单价(元)" v-if="columns[8].visible" prop="unitPrice" align="center" width="80">
        <template #default="scope">
          <span class="red">{{ getTotalCost(scope.row,'unitPrice') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额(元)" v-if="columns[9].visible" prop="totalPrice" align="center" width="80">
        <template #default="scope">
          <span class="red">{{ getTotalCost(scope.row,'totalPrice') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="success" @click="submitForm" icon="Download">导出报价</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  },
   // 接收列表数据
  dataList: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits([
  'update:dialogVisible',
  'submitForm',
]);

const visible = computed({
  get: () => props.dialogVisible,
  set: (val) => emit('update:dialogVisible', val)
});

// 列显隐信息
const columns = ref([
  { key: 0, label: `名称`, visible: true },
  { key: 1, label: `物料号`, visible: false },
  { key: 3, label: `规格`, visible: true },
  { key: 2, label: `型号`, visible: false },
  { key: 4, label: `数量`, visible: true },
  { key: 5, label: `单位`, visible: true },
  { key: 6, label: `采购备注`, visible: false },
  { key: 7, label: `成本`, visible: true },
  { key: 8, label: `总成本`, visible: true },
  { key: 9, label: `单价`, visible: true },
  { key: 10, label: `金额`, visible: true }
]);

const tableRef = ref(null);
const loading = ref(false);

// 计算成本总和并保留两位小数的方法，解决浮点数精度问题
const getTotalCost = (row, type) => {
  // 只有当 row.childMaterial 存在且长度大于 0 时才进行总和计算
  if (row.childMaterial && row.childMaterial.length > 0) {
    if(type === 'totalCost' || type === 'totalPrice') {
      let maxDecimals = row.childMaterial.reduce((max, child) => {
        let decimalLength = String(Number(child[type]) || 0).split('.')[1]?.length || 0;
        return Math.max(max, decimalLength);
      }, 0);
      let multiplier = Math.pow(10, maxDecimals);
      let total = row.childMaterial.reduce((sum, child) => {
        return sum + Math.round((Number(child[type]) || 0) * multiplier);
      }, 0);
      if(type == 'totalPrice'){
        total = total*Number(row.billRemaining);
      }
      return (total / multiplier).toFixed(2);
    }else{
      return '-'
    }
  }
  // 其他情况直接返回接口返回的数据
  return row[type];
};
// 修改
function handleUpdate(row) {
  // 处理修改逻辑
  emit('handleUpdate', row);
}

// 提交表单
function submitForm() {
  // 处理提交表单逻辑
  emit('submitForm');
  visible.value = false;
}

// 取消
function cancel() {
  visible.value = false;
}

// 打开对话框
function open() {
  // 处理打开对话框逻辑
}
</script>

<style lang="scss" scoped>
.el-input-number {
  width: 190px;
}
.red{
  color: red;
}
.el-table__body-wrapper {
  overflow-y: auto;
}

</style>